﻿// Decompiled with JetBrains decompiler
// Type: MCM.Implementation.Global.ExternalGlobalSettingsContainer
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using BUTR.DependencyInjection;
using BUTR.DependencyInjection.Logger;
using MCM.Abstractions;
using MCM.Abstractions.Base;
using MCM.Abstractions.Base.Global;
using MCM.Abstractions.Global;
using System;
using System.Collections.Generic;
using System.Linq;

#nullable enable
namespace MCM.Implementation.Global
{
  internal sealed class ExternalGlobalSettingsContainer : 
    IFluentGlobalSettingsContainer,
    IGlobalSettingsContainer,
    ISettingsContainer,
    ISettingsContainerHasSettingsDefinitions,
    ISettingsContainerCanOverride,
    ISettingsContainerCanReset,
    ISettingsContainerPresets,
    ISettingsContainerHasSettingsPack
  {
    private Dictionary<string, ExternalGlobalSettings> LoadedSettings { get; } = new Dictionary<string, ExternalGlobalSettings>();

    public IEnumerable<SettingsDefinition> SettingsDefinitions
    {
      get
      {
        return this.LoadedSettings.Values.Select<ExternalGlobalSettings, SettingsDefinition>((Func<ExternalGlobalSettings, SettingsDefinition>) (s => new SettingsDefinition(s.Id, s.DisplayName, s.GetSettingPropertyGroups())));
      }
    }

    public ExternalGlobalSettingsContainer(
      IBUTRLogger<ExternalGlobalSettingsContainer> logger)
    {
    }

    public BaseSettings? GetSettings(string id)
    {
      ExternalGlobalSettings externalGlobalSettings;
      return !this.LoadedSettings.TryGetValue(id, out externalGlobalSettings) ? (BaseSettings) null : (BaseSettings) externalGlobalSettings;
    }

    public bool SaveSettings(BaseSettings settings)
    {
      if (!(settings is ExternalGlobalSettings) || !this.LoadedSettings.ContainsKey(settings.Id))
        return false;
      settings.OnPropertyChanged("SAVE_TRIGGERED");
      return true;
    }

    public bool OverrideSettings(BaseSettings settings)
    {
      if (!(settings is ExternalGlobalSettings) || !this.LoadedSettings.ContainsKey(settings.Id))
        return false;
      SettingsUtils.OverrideSettings((BaseSettings) this.LoadedSettings[settings.Id], settings);
      return true;
    }

    public bool ResetSettings(BaseSettings settings)
    {
      if (!(settings is ExternalGlobalSettings) || !this.LoadedSettings.ContainsKey(settings.Id))
        return false;
      SettingsUtils.ResetSettings((BaseSettings) this.LoadedSettings[settings.Id]);
      return true;
    }

    public IEnumerable<ISettingsPreset> GetPresets(string settingsId)
    {
      yield break;
    }

    public void Register(FluentGlobalSettings settings)
    {
      if (!(settings is ExternalGlobalSettings externalGlobalSettings))
        return;
      this.LoadedSettings.Add(externalGlobalSettings.Id, externalGlobalSettings);
      settings.OnPropertyChanged("LOADING_COMPLETE");
    }

    public void Unregister(FluentGlobalSettings settings)
    {
      if (!(settings is ExternalGlobalSettings externalGlobalSettings) || !this.LoadedSettings.ContainsKey(externalGlobalSettings.Id))
        return;
      this.LoadedSettings.Remove(externalGlobalSettings.Id);
    }

    public bool SavePresets(ISettingsPreset preset)
    {
      ExternalGlobalSettings settings;
      return this.LoadedSettings.TryGetValue(preset.SettingsId, out settings) && preset.SavePreset((BaseSettings) settings);
    }

    public IEnumerable<SettingSnapshot> SaveAvailableSnapshots()
    {
      JsonSettingsFormat jsonFormat = GenericServiceProvider.GetService<JsonSettingsFormat>();
      if (jsonFormat != null)
      {
        foreach (ExternalGlobalSettings setting in this.LoadedSettings.Values)
        {
          string json = jsonFormat.SaveJson((BaseSettings) setting);
          string folderName = !string.IsNullOrEmpty(setting.FolderName) ? setting.FolderName + "/" : string.Empty;
          string subFolderName = !string.IsNullOrEmpty(setting.SubFolder) ? setting.SubFolder + "/" : string.Empty;
          string path = folderName + subFolderName + setting.Id + ".json";
          yield return new SettingSnapshot(path, json);
          json = (string) null;
          folderName = (string) null;
          subFolderName = (string) null;
          path = (string) null;
        }
      }
    }

    public IEnumerable<BaseSettings> LoadAvailableSnapshots(IEnumerable<SettingSnapshot> snapshots)
    {
      JsonSettingsFormat jsonFormat = GenericServiceProvider.GetService<JsonSettingsFormat>();
      if (jsonFormat != null)
      {
        Dictionary<string, string> snapshotDict = snapshots.ToDictionary<SettingSnapshot, string, string>((Func<SettingSnapshot, string>) (x => x.Path), (Func<SettingSnapshot, string>) (x => x.JsonContent));
        foreach (ExternalGlobalSettings setting in this.LoadedSettings.Values)
        {
          string folderName = !string.IsNullOrEmpty(setting.FolderName) ? setting.FolderName + "/" : string.Empty;
          string subFolderName = !string.IsNullOrEmpty(setting.SubFolder) ? setting.SubFolder + "/" : string.Empty;
          string path = folderName + subFolderName + setting.Id + ".json";
          string json;
          if (snapshotDict.TryGetValue(path, out json))
            yield return jsonFormat.LoadFromJson(setting.CreateNew(), json);
          folderName = (string) null;
          subFolderName = (string) null;
          path = (string) null;
          json = (string) null;
        }
      }
    }
  }
}
